import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

// https://docs.flutter.dev/get-started/fundamentals/networking#introduction-to-retrieving-data-over-the-network
// https://docs.flutter.dev/cookbook/networking/fetch-data
// https://api.flutter.dev/flutter/painting/BoxDecoration-class.html
// flutter create --sample=material.Card.1 mysample

class MyButton01 extends StatefulWidget {
  final double alpha;
  final Color containerColor;
  final ShapeBorder shape;
  final BorderSide? border;
  final Color textColor;
  final Size size;
  final double fontSize;
  final FontWeight fontWeight;
  final String fontFamily;
  final String text;
  final void Function(String) onClick;

  const MyButton01({
    super.key,
    this.alpha = 1.0,
    this.containerColor = const Color(0xFF000000),
    this.shape = const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))),
    this.border,
    this.textColor = Colors.white,
    this.size = const Size(144, 44),
    this.fontSize = 14.0,
    this.fontWeight = FontWeight.normal,
    this.fontFamily = 'Roboto',
    required this.text,
    required this.onClick,
  });

  @override
  _MyButton01State createState() => _MyButton01State();
}

class _MyButton01State extends State<MyButton01> {
  bool _isPressed = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) {
        setState(() {
          _isPressed = true;
        });
      },
      onTapUp: (_) {
        setState(() {
          _isPressed = false;
        });
        widget.onClick(widget.text);
      },
      onTapCancel: () {
        setState(() {
          _isPressed = false;
        });
      },
      child: Opacity(
        opacity: _isPressed ? widget.alpha * 0.5 : widget.alpha, // 点击时降低透明度
        child: Container(
          width: widget.size.width,
          height: widget.size.height,
          decoration: ShapeDecoration(
            color: widget.containerColor,
            shape: widget.shape,
            shadows: _isPressed
                ? [BoxShadow(color: Colors.black26, blurRadius: 5)] // 点击时增加阴影效果
                : [],
          ),
          child: Center(
            child: Text(
              widget.text,
              style: TextStyle(
                fontSize: widget.fontSize,
                fontWeight: widget.fontWeight,
                color: widget.textColor,
                fontFamily: widget.fontFamily,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class RobotList extends StatelessWidget {
  final double height; // 新增参数
  RobotList({super.key, this.height = 400});

  final List<String> entries = List.generate(1000, (index) => 'Item $index');
  final List<int> colorCodes = <int>[600, 500, 100];

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        height: height, // 指定 ListView 的高度
        child: ListView.builder(
            padding: const EdgeInsets.all(8),
            itemCount: entries.length,
            itemBuilder: (BuildContext context, int index) {
              print("${index}");
              return Container(
                height: 50,
                color: Colors.amber[colorCodes[index % 3]],
                child: Center(child: Text('Entry ${entries[index]}')),
              );
            }));
  }
}

class RobotText extends StatelessWidget {
  final String text; // 新增参数

  const RobotText({super.key, required this.text});

  @override
  Widget build(BuildContext context) {
    return Text(text,
        style: const TextStyle(
          fontSize: 48,
          // 设置字体大小
          color: Colors.blue,
          // 设置字体颜色
          fontWeight: FontWeight.bold,
          // 设置字体粗细
          // fontStyle: FontStyle.italic, // 设置字体样式为斜体
          letterSpacing: 2.0,
          // 设置字母间距
          wordSpacing: 15.0, // 设置单词间距
          // decoration: TextDecoration.underline, // 设置文本下划线
          // decorationColor: Colors.red, // 设置下划线的颜色
          // decorationThickness: 2.0, // 设置下划线的粗细
        ));
  }
}

class RobotButton extends StatelessWidget {
  final void Function(String) onClick;
  const RobotButton({super.key,required this.onClick});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.red[100],
        elevation: 0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8), // 圆角边框
        ),
      ),
      onPressed: () {
        this.onClick('RobotButton');
        print("ElevatedButton onPressed");
      },
      child: Text("RobotButton"),
    );
  }
}

class RobotCard extends StatelessWidget {
  // 在构造函数中添加 child 参数，类型为 Widget
  final Widget child;

  const RobotCard({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        width: 300, // 设置宽度
        height: 300, // 设置高度
        child: Card(
            color: Colors.blueAccent[100], // 设置卡片的背景色
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16.0), // 设置圆角
            ),
            child: Center(child: child)));
  }
}

void showRobotPopup(
  BuildContext context, {
  double x = 400,
  double y = 400,
  required Container child,
}) {
  var screen = MediaQuery.of(context).size.width;
  var screen2 = MediaQuery.of(context).size.height;
  var w = (x / screen) * 2 - 1;
  var h = (y / screen2) * 2 - 1;
  showDialog(
      context: context,
      barrierDismissible: true, // 允许点击外部关闭对话框
      useSafeArea: false,
      builder: (BuildContext context) {
        return Dialog(
          alignment: Alignment(w, h),
          insetPadding: EdgeInsets.zero, // 让对话框填满屏幕
          child: child,
        );
      });
}

/**
 *  类似于 compose的 fillMaxSize
 */
class RobotFillMaxSizeCard extends StatelessWidget {
  final Widget content;

  const RobotFillMaxSizeCard({Key? key, required this.content}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity, // 设置容器宽度填满可用空间
      height: double.infinity, // 设置容器宽度填满可用空间
      padding: const EdgeInsets.only(left: 30.0, right: 30.0,top:30), // 设置左右边距
      decoration: BoxDecoration(
        color: Colors.red,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(40.0), // 设置左上角圆角
          topRight: Radius.circular(40.0), // 设置右上角圆角
          bottomLeft: Radius.circular(40), // 设置左下角为直角
          bottomRight: Radius.circular(40), // 设置右下角为直角
        ),
      ),
      child: content, // 调用传入的内容
    );
  }
}


//用在系统信息的渲染列表,是否渲染成功.
class RobotOutLineText extends StatelessWidget {
  const RobotOutLineText({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 105,
      height: 36,
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(1.0), // 设置背景色，可选
        border: Border.all(
          color: MYBLUE.withOpacity(0.2), // 设置边框颜色为红色
          width: 2,          // 设置边框宽度为 1px
        ),
        borderRadius: BorderRadius.circular(6), // 设置圆角半径
      ),
      child: const Center(
        child: Text('渲染成功',
          style: TextStyle(
            color: MYBLUE, // 文字颜色
            fontSize: 20, // 文字大小
            fontWeight: FontWeight.normal, // 字体加粗
          ),
        ),
      ),
    );
  }
}


const MYBLUE = Color(0xff2c72e5);

